<?php 

/* ------------------------------------------------------------
 * Page: Your Bookings
 * Author: Gustavo Lozano & Fredrik Kronander
 * Description: Booking page where all the books flights are shown to the user
 * 
 **------------------------------------------------------------*/

?>


<?php
	require_once 'source/UtsAirs.php';	
	
	/**
	 * Verify if there is a session or not
	 */
	if(!isset($_SESSION["uts"]))
	{
		$utsAirs = new UtsAirs();	
	}
	else 
	{
		
		$utsAirs = unserialize($_SESSION["uts"]);
		
	}
	
	/**
	 * Check if the request to erase al the book flight was entered
	 */
	if(isset($_REQUEST["botton"]) && $_REQUEST['botton'] == "erase")
	{
		session_destroy();
		$utsAirs->flights = array();
	}

	/**
	 * Check if it comes from adding a flight to the list
	 */
	if(!empty($_REQUEST["flightNo"]))
	{
		$flightAdd = $utsAirs->getFlight($_REQUEST["flightNo"]);

		//i is the seat number, so maximum is 5 seats available
		for($seat = 0; $seat < 5 ; $seat++)
		{		
			if(isset($_REQUEST[$seat]))
			{
				$type = $_REQUEST[$seat];
				foreach ($type as $t)
				{
					//changes the seat of the flight 
					$flightAdd->changeSeat($seat, $t);
				}
			}
		}
		
		$utsAirs->addFlight($flightAdd);
		
	}
	
	//Saves the session again
	$_SESSION["uts"] = serialize($utsAirs);
	$flights = $utsAirs->flights;
	
?>

<link rel="stylesheet" type="text/css" href="css/yourBookings.css" />
<script type="text/javascript">

function booked ()
{
	alert("There are no flights boooked to check out");
}

</script>

Your Bookings

<table width="60%" align="center" border="1">
<?php 
	$booked = TRUE;
	//Check if there are bookes flights or not
	if( count($flights) > 0)
	{
		$booked = TRUE;
?>	
		<tr>
			<td>From</td>
			<td>To</td>
			<td>Seats</td>
		</tr>			
<?php 
		/**
		 * Goes through the flisgts and the seats of each flight and prints them
		 */
		foreach ($flights as $flight)
		{
			echo "<tr>";
			echo "<td> $flight->fromCity </td>";
			echo "<td> $flight->toCity </td>";
			
			$seats = $flight->seats;
			$i = 1;
			echo "<td>";
			//Seats of the flight
			foreach ($seats as $s)
			{
				while (list($k,$v)= each($s))
				{
					if($v == 'YES')
					{
						if($k != CHECKED)
							echo " $k";
						else
							echo "<br>Seat $i:";
					}
				}
				$i++;
			}
			echo "</td>";
			echo "</tr>";
		}

	}
	else
	{
		$booked = FALSE;
		echo "<tr>";
		echo "<td>There are no booked flights</td>";
		echo "</tr>";
		
	}

?>
</table>


<div id="buttons">
	<form id="changesForm">
		<input type="button" id="erase" value="Clear all booked flights" onClick="window.location.href='index.php?content=YourBookings&botton=erase'"/>
		<input type="button" id="checkout" value="Proceed to Checkout" onclick=<?php if(!$booked) echo 'booked()'; else echo "window.location.href='index.php?content=PersonalDetails'"; ?> />
		<input type="button" id= "addmore" value="Book more Flights" onClick="window.location.href='index.php?content=Search_Flights'"/>
	</form>
</div>



